package com.plugin.planilha;

import java.util.HashMap;
import java.util.Map;

import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class AnaliseListSheet implements ISheet {

	private WritableWorkbook workbook;
	private HashMap<String, Map<String, Integer[]>> map;
	private static WritableSheet repeticaoListWS;

	public AnaliseListSheet(WritableWorkbook w,
			HashMap<String, Map<String, Integer[]>> map) {
		workbook = w;
		this.map = map;
		repeticaoListWS = workbook.createSheet("Repeticao em List", 0);
	}

	@Override
	public void criaCabecalho(WritableCellFormat cf)
			throws RowsExceededException, WriteException {
		Label label = new Label(0, 0, "Variavel", cf);
		repeticaoListWS.addCell(label);

		label = new Label(1, 0, "ADDs depois de CONTAINS", cf);
		repeticaoListWS.addCell(label);

		label = new Label(2, 0, "ADDs", cf);
		repeticaoListWS.addCell(label);

		label = new Label(3, 0, "Taxa de comparacao(%)", cf);
		repeticaoListWS.addCell(label);

		label = new Label(4, 0, "Repeticao", cf);
		repeticaoListWS.addCell(label);

		label = new Label(5, 0, "Ordem", cf);
		repeticaoListWS.addCell(label);

		label = new Label(7, 0, "Total ADDS", cf);
		repeticaoListWS.addCell(label);

		label = new Label(8, 0, "Total ADDS depois de CONTAINS", cf);
		repeticaoListWS.addCell(label);

		label = new Label(9, 0,
				"Taxa de comparacao total de ADDs depois de CONTAINS",
				cf);
		repeticaoListWS.addCell(label);

		label = new Label(11, 0, "Total de variaveis corretas", cf);
		repeticaoListWS.addCell(label);

		label = new Label(12, 0, "Total de variaveis mal usadas", cf);
		repeticaoListWS.addCell(label);

		label = new Label(13, 0, "Taxa das variaveis corretas", cf);
		repeticaoListWS.addCell(label);

		// RepeticaoListWS.mergeCells(9, 0, 12, 0);
		// label = new Label(11, 0,
		// "LIST",
		// cf);
		// RepeticaoListWS.addCell(label);
	}

	@Override
	public void addDados() throws RowsExceededException, WriteException {

		Label label;
		jxl.write.Number n;
		int indice = 1;
		double total_add = 0, total_addcontains = 0;
		double total_variaveis_true = 0, total_variaveis_false = 0;

		for (String ch : map.keySet()) {
			if (map.get(ch).size() > 0) {
				for (String variavel : map.get(ch).keySet()) {
					int add_contains = map.get(ch).get(variavel)[0];
					int add = map.get(ch).get(variavel)[1];
					boolean repeticao = (add_contains > 0) ? true : false;
					boolean ordem = ((map.get(ch).get(variavel)[2] > 0) ? true
							: false);

					label = new Label(0, indice, variavel);
					repeticaoListWS.addCell(label);

					n = new jxl.write.Number(1, indice, add_contains);
					repeticaoListWS.addCell(n);

					n = new jxl.write.Number(2, indice, add);
					repeticaoListWS.addCell(n);

					label = new Label(4, indice, Boolean.toString(repeticao));
					repeticaoListWS.addCell(label);

					label = new Label(5, indice, Boolean.toString(ordem));
					repeticaoListWS.addCell(label);

					if (add != 0) {
						int taxa_comparacao = (add_contains * 100) / add;
						n = new jxl.write.Number(3, indice, taxa_comparacao);
						repeticaoListWS.addCell(n);
					} else {
						label = new Label(3, indice, "null");
						repeticaoListWS.addCell(label);
					}
					total_add += add;
					total_addcontains += add_contains;
					if (!repeticao && ordem)
						total_variaveis_true += 1;
					else
						total_variaveis_false += 1;
					indice++;
				}

			}
		}

		double round_total = 0;
		if (total_add != 0) {
			double total = (100 * total_addcontains) / total_add;
			round_total = (double) Math.round(total * 1000) / 1000;
		}

		double round_taxa = 0;
		if ((total_variaveis_true + total_variaveis_false) != 0) {
			double taxa_das_variaveis_corretas = (total_variaveis_true * 100)
					/ (total_variaveis_true + total_variaveis_false);
			round_taxa = (double) Math
					.round(taxa_das_variaveis_corretas * 1000) / 1000;
		}

		n = new jxl.write.Number(7, 1, (int) total_add);
		repeticaoListWS.addCell(n);
		n = new jxl.write.Number(8, 1, (int) total_addcontains);
		repeticaoListWS.addCell(n);
		n = new jxl.write.Number(9, 1, round_total);
		repeticaoListWS.addCell(n);
		n = new jxl.write.Number(11, 1, (int) total_variaveis_true);
		repeticaoListWS.addCell(n);
		n = new jxl.write.Number(12, 1, (int) total_variaveis_false);
		repeticaoListWS.addCell(n);
		n = new jxl.write.Number(13, 1, round_taxa);
		repeticaoListWS.addCell(n);

	}

}
